package com.lqfan.autoAnswerQ.controller.errorRecords;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lqfan.autoAnswerQ.service.errorRecords.IImportErrorRecordsService;
import com.lqfan.autoAnswerQ.utils.ResponseMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  错误管理  控制器
 * </p>
 * @author zml
 * @since 2020-04-09
 */

@RestController
@RequestMapping(value = "/api",produces = MediaType.APPLICATION_JSON_VALUE)
@Api(value = "错误管理的接口")
public class ErrorRecordsController {
    private static final Logger logger = LoggerFactory.getLogger(ErrorRecordsController.class);

    @Autowired
    private IImportErrorRecordsService errorRecordsService;


    @ApiOperation(value = "错误日志列表", notes = "错误日志列表")
    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String")
    @RequestMapping(value = "getErrorRecords", method = RequestMethod.POST)
    public ResponseMessage getErrorRecords(@RequestParam(value = "userId") String userId) {
        logger.info("获取错误管理列表");
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("userId",userId);
        List recordsList = errorRecordsService.list(queryWrapper);
        return ResponseMessage.success(recordsList);
    }


    @ApiOperation(value = "删除对应错误日志", notes = "删除对应错误日志")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "id", value = "对应记录的id", required = true, dataType = "String")
    })
    @RequestMapping(value = "delErrorRecord", method = RequestMethod.POST)
    public ResponseMessage delErrorRecord(@RequestParam(value = "userId") String userId,
                                          @RequestParam(value = "id") String id) {
        logger.info("删除对应错误日志");
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("userId",userId);
        queryWrapper.eq("id",id);
        boolean res = errorRecordsService.remove(queryWrapper);
        if(!res) {
            logger.info("删除对应错误日志失败");
            return ResponseMessage.fail("删除失败");
        }
        logger.info("删除对应错误日志成功");
        return ResponseMessage.success("删除成功");
    }
}
